package com.ruoyi.approval.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.check.domain.Vacate;
import com.ruoyi.check.mapper.VacateMapper;
import com.ruoyi.document.domain.Contract;
import com.ruoyi.document.mapper.ContractMapper;
import com.ruoyi.official.domain.Money;
import com.ruoyi.official.mapper.MoneyMapper;
import com.ruoyi.personnel.domain.Employee;
import com.ruoyi.personnel.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.approval.mapper.ExamineAndApproveMapper;
import com.ruoyi.approval.domain.ExamineAndApprove;
import com.ruoyi.approval.service.IExamineAndApproveService;

/**
 * 审批管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-13
 */
@Service
public class ExamineAndApproveServiceImpl implements IExamineAndApproveService 
{
    @Autowired
    private ExamineAndApproveMapper examineAndApproveMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private VacateMapper vacateMapper;

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private MoneyMapper moneyMapper;

    /**
     * 查询审批管理
     * 
     * @param serialNumber 审批管理主键
     * @return 审批管理
     */
    @Override
    public ExamineAndApprove selectExamineAndApproveBySerialNumber(Integer serialNumber)
    {

        return examineAndApproveMapper.selectExamineAndApproveBySerialNumber(serialNumber);
    }

    /**
     * 查询审批管理列表
     * 
     * @param examineAndApprove 审批管理
     * @return 审批管理
     */
    @Override
    public List<ExamineAndApprove> selectExamineAndApproveList(ExamineAndApprove examineAndApprove)
    {
        List<ExamineAndApprove> examineAndApproveList = examineAndApproveMapper.selectExamineAndApproveList(examineAndApprove);
//        for (ExamineAndApprove approve : examineAndApproveList) {
//            Employee employee = employeeMapper.selectEmployeeByEmpId(approve.getEmpId());
//            approve.setEmpName(employee.getEmpName());
//        }

        return examineAndApproveList;
    }

    /**
     * 查询未审批管理列表
     *
     * @return 审批管理
     */
    @Override
    public List<ExamineAndApprove> selectExamineAndApproveListTwo()
    {
//        ExamineAndApprove examineAndApprove = new ExamineAndApprove();
        List<ExamineAndApprove> examineAndApproveList = examineAndApproveMapper.selectExamineAndApproveListTwo();
        for (ExamineAndApprove approve : examineAndApproveList) {
            Employee employee = employeeMapper.selectEmployeeByEmpId(approve.getEmpId());
            approve.setEmpName(employee.getEmpName());
        }

//        if ("av".equals()){
//
//        }

        return examineAndApproveList;
    }

    /**
     * 新增审批管理
     * 
     * @param examineAndApprove 审批管理
     * @return 结果
     */
    @Override
    public int insertExamineAndApprove(ExamineAndApprove examineAndApprove)
    {
        Date date = new Date();
        examineAndApprove.setNewtime(date);
        return examineAndApproveMapper.insertExamineAndApprove(examineAndApprove);
    }

    /**
     * 修改审批管理
     * 
     * @param examineAndApprove 审批管理
     * @return 结果
     */
    @Override
    public int updateExamineAndApprove(ExamineAndApprove examineAndApprove)
    {
        return examineAndApproveMapper.updateExamineAndApprove(examineAndApprove);
    }

    /**
     * 批量删除审批管理
     * 
     * @param serialNumbers 需要删除的审批管理主键
     * @return 结果
     */
    @Override
    public int deleteExamineAndApproveBySerialNumbers(Integer[] serialNumbers)
    {
        return examineAndApproveMapper.deleteExamineAndApproveBySerialNumbers(serialNumbers);
    }

    /**
     * 删除审批管理信息
     * 
     * @param serialNumber 审批管理主键
     * @return 结果
     */
    @Override
    public int deleteExamineAndApproveBySerialNumber(Integer serialNumber)
    {
        return examineAndApproveMapper.deleteExamineAndApproveBySerialNumber(serialNumber);
    }

    /**
     * 同意审批管理
     *
     * @param examineAndApprove 审批管理
     * @return 结果
     */
    @Override
    public int updateOkExamineAndApprove(ExamineAndApprove examineAndApprove)
    {
        Integer newSerialNumber = examineAndApprove.getSerialNumber();
        ExamineAndApprove oldExamineAndApprove = examineAndApproveMapper.selectExamineAndApproveBySerialNumber(newSerialNumber);
        String remark = oldExamineAndApprove.getRemark();
        String[] strs = remark.split("/");

        if ("vacate".equals(strs[0])){
            int id = Integer.parseInt(strs[1]);
            Vacate oldVacate = vacateMapper.selectVacateByAbId(id);
            oldVacate.setState(2);
            vacateMapper.updateVacate(oldVacate);
        }else if ("contract".equals(strs[0])){
            int id = Integer.parseInt(strs[1]);
            Contract oldContract = contractMapper.selectContractByConId(id);
            oldContract.setState(2);
            contractMapper.updateContract(oldContract);
        }else if ("money".equals(strs[0])){
            int id = Integer.parseInt(strs[1]);
            Money oldMoney = moneyMapper.selectMoneyBySpendMoneyId(id);
            oldMoney.setState(2);
            moneyMapper.updateMoney(oldMoney);
        }
        Date date = new Date();
        examineAndApprove.setUpdatetime(date);
        return examineAndApproveMapper.updateOkExamineAndApprove(examineAndApprove);
    }

    /**
     * 拒绝审批管理
     *
     * @param examineAndApprove 审批管理
     * @return 结果
     */
    @Override
    public int updateNoExamineAndApprove(ExamineAndApprove examineAndApprove)
    {

        Integer newSerialNumber = examineAndApprove.getSerialNumber();
        ExamineAndApprove oldExamineAndApprove = examineAndApproveMapper.selectExamineAndApproveBySerialNumber(newSerialNumber);
        String remark = oldExamineAndApprove.getRemark();
        String[] strs = remark.split("/");

        if ("vacate".equals(strs[0])){
            int id = Integer.parseInt(strs[1]);
            Vacate oldVacate = vacateMapper.selectVacateByAbId(id);
            oldVacate.setState(3);
            vacateMapper.updateVacate(oldVacate);
        }else if ("contract".equals(strs[0])){
            int id = Integer.parseInt(strs[1]);
            Contract oldContract = contractMapper.selectContractByConId(id);
            oldContract.setState(3);
            contractMapper.updateContract(oldContract);
        }else if ("money".equals(strs[0])){
            int id = Integer.parseInt(strs[1]);
            Money oldMoney = moneyMapper.selectMoneyBySpendMoneyId(id);
            oldMoney.setState(3);
            moneyMapper.updateMoney(oldMoney);
        }
        Date date = new Date();
        examineAndApprove.setUpdatetime(date);
        return examineAndApproveMapper.updateNoExamineAndApprove(examineAndApprove);
    }

    @Override
    public void rollbackEmployee(Object[] array) {
        examineAndApproveMapper.rollbackEmployee(array);
    }
}
